package com.gz.mylife.app;

import java.lang.ref.WeakReference;

import com.gz.mylife.Define;
import com.gz.mylife.R;
import com.loopj.android.http.RequestParams;
import com.zl.smartmall.library.listener.CheckPhoneNumber;
import com.zl.smartmall.library.util.Constants;
import com.zl.smartmall.library.util.CrashHandler;
import com.zl.smartmall.library.util.MD5;
import com.zl.smartmall.library.util.SingtonAsyncHttpClient;
import com.zl.smartmall.library.util.Tools;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class Register2 extends BaseActivity implements OnClickListener {
	private String phone;
	private String smscode;
	private int type;

	private EditText mEtSmsCode;
	private EditText mEtPassword;
	private EditText mEtPassRepeat;
	private TextView mTvResend;
	private TextView mTvLoading;
	private Dialog mLoadingData;

	private MyHandler mHandler;

	private int time = 59;

	private MyCheckListener mListener;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register2);
		phone = getIntent().getStringExtra("phone");
		smscode = getIntent().getStringExtra("smscode");
		type = getIntent().getIntExtra("type", 0);

		initView();
		switch (type) {
		case 0:
			//			mActionBar.setTitle("新账号");
			setActionbarTitle("新账号", false);
			mEtSmsCode.setHint("输入短信中的验证码");
			mEtPassword.setHint("密码位数不少于6位");
			mEtPassRepeat.setHint("重新输入密码");
			break;
		default:
			//			mActionBar.setTitle("密码找回");
			setActionbarTitle("密码找回", false);
			mEtSmsCode.setHint("输入短信中的验证码");
			mEtPassword.setHint("新密码位数不少于6位");
			mEtPassRepeat.setHint("重新输入新密码");
			break;
		}


		mHandler = new MyHandler(this);
		mListener = new MyCheckListener(this);

		mTvResend.setText("重新发送("+time+")");
		mTvResend.setTextColor(getResources().getColor(R.color.global_orange));

		//		mEtSmsCode.setText(smscode);


		mHandler.postDelayed(leftTime, 1000);
	}


	private void initView() {
		mLoadingData = new Dialog(this, R.style.DialogTheme);
		mLoadingData = new Dialog(this, R.style.DialogTheme);
		View loadView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.loading_data_dialog2, null);
		mLoadingData.setContentView(loadView);
		mTvLoading = (TextView) loadView.findViewById(R.id.show_tv);
		mLoadingData.setCanceledOnTouchOutside(true);

		mEtSmsCode = (EditText) findViewById(R.id.et_smscode);
		mEtPassword = (EditText) findViewById(R.id.et_password);
		mEtPassRepeat = (EditText) findViewById(R.id.et_password_repeat);
		mTvResend = (TextView) findViewById(R.id.tv_resend);
		mTvResend.setClickable(false);
		mTvResend.setOnClickListener(this);
		findViewById(R.id.btn_update).setOnClickListener(this);
	}


	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_update:
			String smscodeStr = mEtSmsCode.getText().toString();
			String password = mEtPassword.getText().toString();
			String passwordRepeat = mEtPassRepeat.getText().toString();

			if (TextUtils.isEmpty(smscodeStr)) {
				Toast.makeText(this, "请输入验证码", Toast.LENGTH_SHORT).show();
				break;
			}

			//			if (!smscodeStr.equals(smscode)) {
			//				Toast.makeText(this, "验证码输入有误", Toast.LENGTH_SHORT).show();
			//				break;
			//			}
			//			
			switch (type) {
			case 0:
				//				if (TextUtils.isEmpty(password)) {
				//					Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
				//					break;
				//				}
				if (password.length()<6) {
					Toast.makeText(this, "密码位数不少于6位", Toast.LENGTH_SHORT).show();
					break;
				}

				if (!password.equals(passwordRepeat)) {
					Toast.makeText(this, "两次密码不一致", Toast.LENGTH_SHORT).show();
					break;
				}

				Intent intent = new Intent(getApplicationContext(), Register3.class);
				intent.putExtra("smscode", smscodeStr);
				intent.putExtra("phone", phone);
				intent.putExtra("password", MD5.md5(password));
				startActivity(intent);
				break;

			default:
				//				if (TextUtils.isEmpty(password)) {
				//					Toast.makeText(this, "请输入新密码", Toast.LENGTH_SHORT).show();
				//					break;
				//				}
				if (password.length()<6) {
					Toast.makeText(this, "新密码位数不少于6位", Toast.LENGTH_SHORT).show();
					break;
				}

				if (!password.equals(passwordRepeat)) {
					Toast.makeText(this, "两次密码不一致", Toast.LENGTH_SHORT).show();
					break;
				}

				mLoadingData.show();
				mTvLoading.setText("请求中...");
				changePassword();
				break;
			}

			break;
		case R.id.tv_resend:
			mLoadingData.show();
			mTvLoading.setText("验证码发送中");
			switch (type) {
			case 0:
				loadDataFromSever();
				break;
			default:
				forgetPassword();
				break;
			}

			break;

		default:
			break;
		}
	}


	private void changePassword() {
		if (!Tools.hasInternetConnection(getApplicationContext())) {
			mLoadingData.dismiss();
			Toast.makeText(this, Define.MSG_NO_NETWORK, Toast.LENGTH_SHORT)
			.show();
			return;
		}
		String url = Constants.URL_CHANGE_PASSWORD;
		RequestParams params = new RequestParams();
		params.put("phone", phone);
		params.put("smscode", mEtSmsCode.getText().toString());
		params.put("password", MD5.md5(mEtPassword.getText().toString()));
		mListener.setmReqType(CheckPhoneNumber.CHANGE_PASSWORD);
		SingtonAsyncHttpClient.getInstance().post(url, params, mListener);
	}

	private void loadDataFromSever() {
		if (!Tools.hasInternetConnection(getApplicationContext())) {
			mLoadingData.dismiss();
			Toast.makeText(this, Define.MSG_NO_NETWORK, Toast.LENGTH_SHORT)
			.show();
			return;
		}
		String url = Constants.URL_CHECK_PHOME;
		RequestParams params = new RequestParams();
		params.put("phone", phone);
		mListener.setmReqType(CheckPhoneNumber.CHECK_PHONE_NUMBER);
		SingtonAsyncHttpClient.getInstance().post(url, params, mListener);
	}
	private void forgetPassword() {
		if (!Tools.hasInternetConnection(getApplicationContext())) {
			mLoadingData.dismiss();
			Toast.makeText(this, Define.MSG_NO_NETWORK, Toast.LENGTH_SHORT)
			.show();
			return;
		}
		String url = Constants.URL_GET_PASSWORD;
		RequestParams params = new RequestParams();
		params.put("phone", phone);
		mListener.setmReqType(CheckPhoneNumber.GET_RESETPASSWORD_SMSCODE);
		SingtonAsyncHttpClient.getInstance().post(url, params, mListener);
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		mHandler.removeCallbacks(leftTime);
		mHandler.release();
		mListener.release();
	}
	private Runnable leftTime = new Runnable() {

		@Override
		public void run() {
			if(time>0){
				mTvResend.setText("重新发送("+time+")");
				mHandler.postDelayed(leftTime, 1000);
				time--;
			}else{
				mTvResend.setText("重新发送");
				mTvResend.setClickable(true);
				mHandler.removeCallbacks(leftTime);
			}

		}
	};

	private static class MyHandler extends Handler {
		private Register2 mOuter;

		public MyHandler(Register2 outer) {
			this.mOuter = outer;
		}

		public void release() {
			new WeakReference<Register2>(mOuter);
		}

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 0:
				break;

			default:
				break;
			}
		}
	}
	private static class MyCheckListener extends CheckPhoneNumber {
		private Register2 mOuter;

		public MyCheckListener(Register2 outer) {
			this.mOuter = outer;
		}

		public void release() {
			new WeakReference<Register2>(mOuter);
		}

		@Override
		public void onResponseSuccess(String phone, String smsCode) {
			if(mOuter == null){
				return;
			}
			CrashHandler.d("sms", smsCode);
			switch (mReqType) {
			case CHECK_PHONE_NUMBER:
			case GET_RESETPASSWORD_SMSCODE:
				mOuter.mEtSmsCode.setText("");
				mOuter.smscode = smsCode;
				mOuter.mLoadingData.dismiss();
				mOuter.mTvResend.setClickable(false);
				mOuter.mTvResend.setText("重新发送("+60+")");
				mOuter.time = 59;
				mOuter.mHandler.postDelayed(mOuter.leftTime, 1000);
				break;
			case CHANGE_PASSWORD:
				Intent exit = new Intent(Constants.ACTION_EXIT_WHOLE_APP);
				mOuter.sendBroadcast(exit);
				mOuter.startActivity(new Intent(mOuter.getApplicationContext(), Login.class));
				mOuter.mLoadingData.dismiss();
				Toast.makeText(mOuter.getApplicationContext(), "提交成功", Toast.LENGTH_SHORT).show();
				break;

			default:
				break;
			}

		}

		@Override
		public void onPhoneError() {
			if(mOuter == null){
				return;
			}
			switch (mReqType) {
			case CHECK_PHONE_NUMBER:
			case GET_RESETPASSWORD_SMSCODE:
				mOuter.mLoadingData.dismiss();
				Toast.makeText(mOuter.getApplicationContext(), "您输入的电话号码有误", 0).show();
				break;
			case CHANGE_PASSWORD:
				mOuter.mLoadingData.dismiss();
				Toast.makeText(mOuter.getApplicationContext(), "提交失败，请重新填写", 0).show();
				break;

			default:
				break;
			}
		}

		@Override
		public void onPhoneIsUsed() {
			if(mOuter == null){
				return;
			}
			switch (mReqType) {
			case CHECK_PHONE_NUMBER:
				mOuter.mLoadingData.dismiss();
				Toast.makeText(mOuter.getApplicationContext(), "该手机号已注册", 0).show();
				break;
			case GET_RESETPASSWORD_SMSCODE:
				mOuter.mLoadingData.dismiss();
				Toast.makeText(mOuter.getApplicationContext(), "该手机号未注册", 0).show();
				break;

			default:
				break;
			}

		}

		@Override
		public void onResponseFailed() {
			if(mOuter == null){
				return;
			}
			switch (mReqType) {
			case CHECK_PHONE_NUMBER:
			case GET_RESETPASSWORD_SMSCODE:
			case CHANGE_PASSWORD:
				mOuter.mLoadingData.dismiss();
				Toast.makeText(mOuter.getApplicationContext(), "网络请求失败", 0).show();
				break;


			default:
				break;
			}

		}

		@Override
		public void onNoNetWork() {

		}

		@Override
		public void passthree() {
			// TODO Auto-generated method stub
			mOuter.mLoadingData.dismiss();
			Toast.makeText(mOuter.getApplicationContext(), "同一手机号当天发送验证码不能超过5次", 0).show();
		}

		@Override
		public void passtwo() {
			// TODO Auto-generated method stub
			mOuter.mLoadingData.dismiss();
			Toast.makeText(mOuter.getApplicationContext(), "同一ip手机当天发送验证码不能超过10次", 0).show();
		}


	}
}
